//
//  IPPastViewController.swift
//  IPlan
//
//  Created by robot on 3/19/16.
//  Copyright © 2016 codeFighter.com. All rights reserved.
//

import UIKit
import SnapKit
import RealmSwift

class IPPastViewController: UIViewController {
    
    lazy var pastPlanTableView = UITableView()
    var  pastNotes:[IPPlaneModel] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "计划"
        view.backgroundColor = UIColor.white
         setUpTableView()
        // Do any additional setup after loading the view.
        
        
    }
   
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        fetchData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func setUpTableView()->Void{
    
        pastPlanTableView.delegate = self
        pastPlanTableView.dataSource = self
        view.addSubview(pastPlanTableView)
        
        pastPlanTableView.snp.makeConstraints { (make) in
            
            make.edges.equalTo(self.view);
            
        }
        
    
    }
    
    func fetchData(){
    
        let realm = try! Realm()
        let results = realm.objects(IPPlaneModel.self)
        pastNotes = Array(results)
        pastPlanTableView.reloadData()
        
    }

}

// MARK: UITableView Delegate UITableViewDataSource
extension IPPastViewController:UITableViewDelegate,UITableViewDataSource{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return pastNotes.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 50
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var pastCell = tableView.dequeueReusableCell(withIdentifier: "pastCell")
        if pastCell == nil{
        
          pastCell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "pastCell")
          
        }
        
        let model = self.pastNotes[indexPath.row]
        pastCell?.textLabel?.text = model.content
        pastCell?.imageView?.image = UIImage.init(named: "me_smail_icon")
        pastCell?.textLabel?.numberOfLines = 2
        pastCell?.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        pastCell?.selectionStyle = .none
        pastCell?.detailTextLabel?.text = "备注"
        return pastCell!
    }
    
    

}
